package com.minggo.charmword.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.annotation.SuppressLint;
import android.util.Log;

/**
 * 时间[避免时间过期方法使用]
 * 
 * @author minggo
 * @date 2013-8-7下午04:35:30
 */
public class MinggoDate {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String dateStr;

	private Date date;

	private Calendar calendar;

	public MinggoDate() {
		date = new Date();
		this.toString();
		calendar = Calendar.getInstance();
	}

	/**
	 * 格式化成yyyy-MM-dd hh:mm:ss
	 */
	public String toString() {
		SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

		dateStr = fm.format(date);
		return dateStr;
	}

	/**
	 * 获取long类型的time
	 * 
	 * @return
	 */
	public long getTimeOfLong() {
		return date.getTime();
	}

	/**
	 * 获取年份
	 */
	public int getYear() {
		return Integer.parseInt(dateStr.substring(0, 4));
	}

	/**
	 * 获取月份
	 */
	public int getMonth() {
		return Integer.parseInt(dateStr.substring(5, 7));
	}

	/**
	 * 获取日
	 */
	public int getDay() {
		return Integer.parseInt(dateStr.substring(8, 10));
	}

	/**
	 * 获取小时
	 * 
	 * @return
	 */
	public int getHour() {
		return Integer.parseInt(dateStr.substring(11, 13));
	}

	/**
	 * 获取小时[24]
	 * 
	 * @return
	 */
	public int get24Hour() {
		return calendar.get(Calendar.HOUR_OF_DAY);
	}

	/**
	 * 获取分
	 * 
	 * @return
	 */
	public int getMinutes() {
		return Integer.parseInt(dateStr.substring(14, 16));
	}

	/**
	 * 获取秒
	 */
	public int getSeconds() {
		return Integer.parseInt(dateStr.substring(17, 19));
	}

	/**
	 * 获取星期
	 * 
	 * @return
	 */
	public int getDayOfWeek() {
		return calendar.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 获取字符串的星期
	 * 
	 * @return
	 */
	public String getWeek2str() {
		String day = "";
		switch (calendar.get(Calendar.DAY_OF_WEEK)) {
		case 1:
			day = "星期天";
			break;
		case 2:
			day = "星期一";
			break;
		case 3:
			day = "星期二";
			break;
		case 4:
			day = "星期三";
			break;
		case 5:
			day = "星期四";
			break;
		case 6:
			day = "星期五";
			break;
		case 7:
			day = "星期六";
			break;
		default:
			break;
		}
		return day;
	}

	/**
	 * 获取英文字符串的星期
	 * 
	 * @return
	 */
	public String getWeek2ENstr() {

		String day = "";
		switch (calendar.get(Calendar.DAY_OF_WEEK)) {
		case 1:
			day = SharePreferenceUtil.SUNDAY_FELLING;
			break;
		case 2:
			day = SharePreferenceUtil.MONDAY_FELLING;
			break;
		case 3:
			day = SharePreferenceUtil.TUESDAY_FELLING;
			break;
		case 4:
			day = SharePreferenceUtil.WEDNESDAY_FELLING;
			break;
		case 5:
			day = SharePreferenceUtil.THURSDAY_FELLING;
			break;
		case 6:
			day = SharePreferenceUtil.FRIDAY_FELLING;
			break;
		case 7:
			day = SharePreferenceUtil.SATURDAY_FELLING;
			break;
		default:
			break;
		}
		return day;
	}

	public static Date today = new Date();

	/**
	 * 判断是不是今天
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isToday(Date date) {
		if (today == null) {
			today = new Date();
		}
		if ((today.getTime() - date.getTime()) <= 0) { // 至少是今天
			return true;
		}
		return false;
	}

	/**
	 * 判断是否今天
	 * 
	 * @param date
	 * @return
	 */
	public static synchronized boolean isToday(long date) {
		if (today == null) {
			today = new Date();
		}
		if ((today.getTime() - date) <= 0) { // 至少是今天
			return true;
		}
		return false;
	}

	/**
	 * 判断是不是昨天
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	@SuppressLint("SimpleDateFormat")
	public static boolean isYesterday(Date date) {

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String newDateStr = format.format(date);
		Date newDate = null;
		try {
			newDate = format.parse(newDateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		;
		// System.out.println(today.getTime());
		if (today.compareTo(newDate) == 1) {// 今天比传来的时间大
			if ((today.getTime() - newDate.getTime()) > 0) {
				System.out.println((today.getTime() - newDate.getTime()));
				if ((today.getTime() - date.getTime()) <= 24 * 60 * 60 * 1000) {
					return true;
				}
			}

		}
		return false;
	}

	/**
	 * 是不是昨天
	 * 
	 * @param date
	 * @return
	 */
	public static boolean isYesterday(long date) {

		if ((today.getTime() - date) > 0 && (today.getTime() - date) <= 86400000) {
			return true;
		}
		return false;
	}

	/**
	 * 时间格式化2014-09-01
	 * 
	 * @param date
	 * @return
	 */
	@SuppressLint("SimpleDateFormat")
	public static String dateFormat0(Date date) {
		return (new java.text.SimpleDateFormat("yyyy-MM-dd")).format(date);
	}

	/**
	 * 时间格式化2014-09-01 12:30:31
	 * 
	 * @param date
	 * @return
	 */
	@SuppressLint("SimpleDateFormat")
	public static String dateFormat1(Date date) {
		return (new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format(date);
	}

	/**
	 * @author LuoB.
	 * @param oldTime
	 *            较小的时间
	 * @param newTime
	 *            较大的时间 (如果为空 默认当前时间 ,表示和当前时间相比)
	 * @return -1 ：同一天. 0：昨天 . 1 ：至少是前天.
	 * @throws ParseException
	 *             转换异常
	 */
	public static int isYeaterday(Date oldTime) {

		try {
			// 昨天 86400000=24*60*60*1000 一天
			if ((today.getTime() - oldTime.getTime()) > 0 && (today.getTime() - oldTime.getTime()) <= 86400000) {
				return 0;
			} else if ((today.getTime() - oldTime.getTime()) <= 0) { // 至少是今天
				return -1;
			} else { // 至少是前天
				return 1;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}
}
